package com.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ManageJdbc {

    Connection conn;
    Statement stem;
    ResultSet rs ;
    public  void  lianjie (){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //数据库用户名密码要写自己的
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ResultSet show(){
        this.lianjie();
        // 向数据库发送sql 语句的
        try {
            stem = conn.createStatement();
            rs = stem.executeQuery(" select  * from  user ");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  rs;
    }

    public  void  close(){
        try {
            if(conn!=null){
                conn.close();
            }

            if(stem!=null){
                stem.close();
            }

            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void add(String username,String password) {
        this.lianjie();
        try {
            stem = conn.createStatement();
            stem.executeUpdate("insert into user values ('"+username+"' , '"+password+"'  ) ");
        }catch (SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.close();
    }

    public void delete(String id){
        this.lianjie();
        try {
            stem = conn.createStatement();
            stem.executeUpdate("delete from user  where uid = '"+id+"'") ;
        }catch (SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.close();
    }

    public void update(String uid, String username, String password) {
        this.lianjie();
        try {
            stem = conn.createStatement();
            stem.executeUpdate("update user set username='"+username+"' , password='"+password+"'  where uid='"+uid+"' ");
        }catch (SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.close();
    }
}

